package diploma.kisel.modelas.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import diploma.kisel.modelas.domain.Computer;
import diploma.kisel.modelas.domain.ComputerType;
import diploma.kisel.modelas.service.ComputerServiceImpl;
import diploma.kisel.modelas.service.api.ComputerService;
import diploma.kisel.modelas.validation.Validation;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This window is designed for describe new computer and saved it
 * into database.
 * 
 * @author Nikolay Kisel
 *
 */
public class ComputerWindow extends JFrame {
	
	private final int computerNumberLength = 10;
	private final int computerBrandLength = 45;
	private final int computerFeaturesLength = 250;
	
	private ComputerService computerService = new ComputerServiceImpl();
	
	private static ComputerWindow frame;
	
	private JPanel contentPane;
	private JTextField textFieldNumber;
	private JTextField textFieldBrand;
	private JTextField textFieldCost;
	private JRadioButton rdbtnS;
	private JRadioButton rdbtnU;
	private JTextArea textArea;
	private JButton btnDelete;
	
	private Computer computer;

	/**
	 * Create the frame.
	 */
	public ComputerWindow() {
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 492, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		JLabel label = new JLabel("Номер:");
		contentPane.add(label);
		
		textFieldNumber = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 3, SpringLayout.NORTH, textFieldNumber);
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldNumber, 10, SpringLayout.NORTH, contentPane);
		contentPane.add(textFieldNumber);
		textFieldNumber.setColumns(10);
		
		JLabel label_1 = new JLabel("Марка:");
		sl_contentPane.putConstraint(SpringLayout.WEST, label_1, 10, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, label, 0, SpringLayout.EAST, label_1);
		contentPane.add(label_1);
		
		textFieldBrand = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldNumber, 0, SpringLayout.WEST, textFieldBrand);
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldBrand, -3, SpringLayout.NORTH, label_1);
		contentPane.add(textFieldBrand);
		textFieldBrand.setColumns(10);
		
		JLabel label_2 = new JLabel("Стоимость:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_2, 132, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, label_1, -44, SpringLayout.NORTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_2, 10, SpringLayout.WEST, contentPane);
		contentPane.add(label_2);
		
		textFieldCost = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldBrand, 0, SpringLayout.WEST, textFieldCost);
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldCost, -3, SpringLayout.NORTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldCost, 25, SpringLayout.EAST, label_2);
		contentPane.add(textFieldCost);
		textFieldCost.setColumns(10);
		
		rdbtnS = new JRadioButton("Cерверный");
		sl_contentPane.putConstraint(SpringLayout.WEST, rdbtnS, 10, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, rdbtnS, -40, SpringLayout.SOUTH, contentPane);
		contentPane.add(rdbtnS);
		
		rdbtnU = new JRadioButton("Пользовательский");
		sl_contentPane.putConstraint(SpringLayout.NORTH, rdbtnU, 0, SpringLayout.NORTH, rdbtnS);
		sl_contentPane.putConstraint(SpringLayout.WEST, rdbtnU, 0, SpringLayout.WEST, textFieldNumber);
		contentPane.add(rdbtnU);
		
		ButtonGroup butGroup = new ButtonGroup();
		butGroup.add(rdbtnS);
		butGroup.add(rdbtnU);
		
		JLabel label_3 = new JLabel("Характеристика:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_3, 0, SpringLayout.NORTH, label);
		contentPane.add(label_3);
		
		JScrollPane scrollPane = new JScrollPane();
		sl_contentPane.putConstraint(SpringLayout.NORTH, scrollPane, 16, SpringLayout.SOUTH, label_3);
		sl_contentPane.putConstraint(SpringLayout.WEST, scrollPane, 62, SpringLayout.EAST, textFieldBrand);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, scrollPane, -139, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, scrollPane, -17, SpringLayout.EAST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_3, 0, SpringLayout.WEST, scrollPane);
		contentPane.add(scrollPane);
		
		textArea = new JTextArea();
		//contentPane.add(textArea);
		scrollPane.setViewportView(textArea);
		
		JButton btnSave = new JButton("Save");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnSave, -10, SpringLayout.SOUTH, contentPane);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveComputer();
			}
		});
		contentPane.add(btnSave);
		
		btnDelete = new JButton("Delete");
		sl_contentPane.putConstraint(SpringLayout.NORTH, btnDelete, 0, SpringLayout.NORTH, btnSave);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnDelete, -6, SpringLayout.WEST, btnSave);
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				deleteComputer();
			}
		});
		contentPane.add(btnDelete);
		
		JButton btnCancel = new JButton("Cancel");
		sl_contentPane.putConstraint(SpringLayout.EAST, btnSave, -6, SpringLayout.WEST, btnCancel);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnCancel, -10, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnCancel, 0, SpringLayout.EAST, scrollPane);
		contentPane.add(btnCancel);
		
		//setFields();
	}

	public Computer getComputer() {
		return computer;
	}

	public void setComputer(Computer computer) {
		this.computer = computer;
	}
	
	public void init() {
		if (computer!=null) {
			textFieldNumber.setText(computer.getNumber());
			textFieldBrand.setText(computer.getBrand());
			textFieldCost.setText(Integer.toString(computer.getCost()));
			if (computer.getType()==ComputerType.Server)
				rdbtnS.setSelected(true);
			else
				rdbtnU.setSelected(true);
			textArea.setText(computer.getFeatures());
		}
	}
		
	public void saveComputer() {
		String fieldNumber = textFieldNumber.getText();
		if (Validation.isLengthBetween(fieldNumber, 1, computerNumberLength))
			computer.setNumber(fieldNumber);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверное название компьютера. Название должно быть до 10 символов.");
			return;
		}
		String fieldBrand = textFieldBrand.getText();
		if (Validation.isLengthBetween(fieldBrand, 1, computerBrandLength))
			computer.setBrand(fieldBrand);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверное название марки. Название должно быть до 45 символов.");
			return;
		}
		String fieldFeatures = textArea.getText();
		if (Validation.isLengthBetween(fieldFeatures, 0, computerFeaturesLength))
			computer.setFeatures(fieldFeatures);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неверное описание характеристик. Характеристика может включать до 250 символов");
			return;
		}
		computer.setFeatures(fieldFeatures);
		String fieldCost = textFieldCost.getText();
		if (Validation.isNumber(fieldCost))
			computer.setCost(Integer.valueOf(fieldCost));
		else {
			JOptionPane.showMessageDialog(frame,
					"Указана неверная цена");
			return;
		}
		if (rdbtnS.isSelected())
			computer.setType(ComputerType.Server);
		else
			computer.setType(ComputerType.User);
		computerService.save(computer);
		JOptionPane.showMessageDialog(frame,
				"Компьютер сохранен");
		ComputerCatalogWindow.setNewModel(computerService.findAll());
		
	}
	
	public void deleteComputer() {
		int response = JOptionPane.showConfirmDialog(frame, "Удалить данный компьютер?");
		if (response == JOptionPane.YES_OPTION) {
//			computerService.remove(computer.getId());
			ComputerCatalogWindow.setNewModel(computerService.findAll());
			frame.setVisible(false);
		}
			
	}

	public static ComputerWindow getFrame() {
		return frame;
	}

	public static void setFrame(ComputerWindow frame) {
		ComputerWindow.frame = frame;
	}

	public JButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(JButton btnDelete) {
		this.btnDelete = btnDelete;
	}	
	
}
